﻿// CElementTabView.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "ElementTabView.h"


// CElementTabView

IMPLEMENT_DYNCREATE(CElementTabView, CTabView)

CElementTabView::CElementTabView()
{

}

CElementTabView::~CElementTabView()
{
}

BEGIN_MESSAGE_MAP(CElementTabView, CTabView)
	ON_WM_CREATE()
END_MESSAGE_MAP()


// CElementTabView 诊断

#ifdef _DEBUG
void CElementTabView::AssertValid() const
{
	CTabView::AssertValid();
}

#ifndef _WIN32_WCE
void CElementTabView::Dump(CDumpContext& dc) const
{
	CTabView::Dump(dc);
}
#endif
#endif //_DEBUG


// CElementTabView 消息处理程序


int CElementTabView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CTabView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CRect rectDummy;
	rectDummy.SetRectEmpty();

	if (!m_wndElementsList.Create(WS_CHILD, rectDummy, &m_wndTabs, 2))
	{
		TRACE0("未能创建检查结果输出窗口\n");
		return -1;      // 未能创建
	}
	m_wndElementsList.ModifyStyle(0, LVS_REPORT | LVS_SINGLESEL);
	m_wndElementsList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_AUTOSIZECOLUMNS);
	m_wndElementsList.InsertColumn(0, "名称", LVCFMT_LEFT, 150);
	m_wndElementsList.InsertColumn(1, "编码", LVCFMT_LEFT, 150);
	m_wndElementsList.InsertColumn(2, "类别", LVCFMT_LEFT, 150);
	m_wndElementsList.InsertColumn(3, "描述", LVCFMT_LEFT, 150);


	if (!m_wndElementInfoList.Create(WS_CHILD, rectDummy, &m_wndTabs, 2))
	{
		TRACE0("未能创建检查结果输出窗口\n");
		return -1;      // 未能创建
	}
	m_wndElementInfoList.ModifyStyle(0, LVS_REPORT | LVS_SINGLESEL);
	m_wndElementInfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_AUTOSIZECOLUMNS);
	m_wndElementInfoList.InsertColumn(0, "属性", LVCFMT_LEFT, 150);
	m_wndElementInfoList.InsertColumn(1, "值", LVCFMT_LEFT, 300);

	m_wndTabs.AddTab(&m_wndElementsList, "列表", (UINT)0);
	m_wndTabs.AddTab(&m_wndElementInfoList, "属性", (UINT)0);

	return 0;
}


void CElementTabView::Populate(unsigned int ckCode, CString strText)
{
	PopulateChildrenElements(ckCode);
	PopulateElemetInfo(ckCode);
}

void CElementTabView::PopulateChildrenElements(unsigned int ckCode)
{
	CListCtrl& list = m_wndElementsList;

	list.DeleteAllItems();
	//list.InsertItem(list.GetItemCount(), strText);

	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	ck::ElementRepository* pElemManager = ck::ElementRepository::GetInstance();
	pElements = pElemManager->GetChildElements(ckCode);
	pElements->Reset();
	while ((pElement = pElements->Next()))
	{
		AddCheckElement(pElement);
	}

	pElements->Release();
}

void CElementTabView::AddCheckElement(ck::CheckElement* pElement)
{
	CListCtrl& list = m_wndElementsList;

	int nItem = 0;
	nItem = list.InsertItem(list.GetItemCount(), pElement->GetName());
	list.SetItemText(nItem, 1, pElement->GetCodeString());
	//list.SetItemText(nItem, 2, "类别");
	list.SetItemText(nItem, 3, pElement->GetDescription());
}

void CElementTabView::PopulateElemetInfo(unsigned int ckCode)
{
	m_wndElementInfoList.DeleteAllItems();

	int nItem = 0;
	ck::CheckElement* pElement = NULL;
	ck::ElementRepository* pElemManager = ck::ElementRepository::GetInstance();
	pElement = pElemManager->GetElementByCode(ckCode);
	if(pElement)
	{
		nItem = m_wndElementInfoList.InsertItem(0,"名称");
		m_wndElementInfoList.SetItemText(nItem, 1, pElement->GetName());
		nItem = m_wndElementInfoList.InsertItem(1, "编码");
		m_wndElementInfoList.SetItemText(nItem, 1, pElement->GetCodeString());
		nItem = m_wndElementInfoList.InsertItem(2, "名称");
		//m_wndElementInfoList.SetItemText(nItem, 1, pElement->GetType());
		nItem = m_wndElementInfoList.InsertItem(3, "类别");
		m_wndElementInfoList.SetItemText(nItem, 1, pElement->GetDescription());

		pElement->Release();
	}

}